package framework;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.sort.SortBuilder;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/10/12.
 */
public interface ElasticSearchTemplate {

    /**
     * 创建或者是修改索引中的表结构
     * @param indexName dbname_type 最好是
     * @param type
     * @param mappingJson
     * @throws Exception
     */
    public  boolean createMapping(String indexName, String type, String mappingJson) throws Exception;

    /**
     * 添加或者是更新数据
     * @param indexName
     * @param typeName
     * @param primaryKey
     * @param jsonData
     */
    public  void addOrUpdate(String indexName, String typeName, String primaryKey, String jsonData);

    /**
     * 添加或者是更新数据
     * @param indexName 索引名称
     * @param typeName 表名
     * @param primaryKeyName 主键属性名
     */
    public String batchAddOrUpdate(List<Map<String, Object>> list, String indexName, String typeName, String primaryKeyName) throws JsonProcessingException;

    /**
     * 根据主键删除
     * @param indexName
     * @param typeName
     * @param primaryKey
     */
    public boolean deleteByPrimaryKey(String indexName, String typeName, String primaryKey);

    /** 主键批量删除
     * 根据主键删除
     * @param indexName
     * @param typeName
     * @param primaryKeyList
     */
    public boolean deleteByPrimaryKeyList(String indexName, String typeName, List<String> primaryKeyList);

    /**
     * 默认官方的会删除索引下的所有条件，所有建议一个type一个索引的建立
     * @param indexName
     * @param typeName
     * @param deleteContidionBuilder
     * @return
     */
    public boolean deleteByCondition(String indexName, String typeName, QueryBuilder deleteContidionBuilder);

    /**
     * 根据主键查询记录
     * @param indexName
     * @param type
     * @param primaryKey
     * @return
     */
    public Map<String, Object> getSingleRecordByPrimaryKey(String indexName, String type, String primaryKey);

    /**
     * 查询列表数据（列表和总数）
     * @param queryBuilder
     * @param indexName
     * @param type
     * @param startCount
     * @param record
     * @return
     */
    public EsWrapperSearchResponse searchRecordList(QueryBuilder queryBuilder, String indexName, String type, int startCount, int record, SortBuilder... sortBuilderList);

    /**
     * 返回原声的结果
     * @param queryBuilder
     * @param indexName
     * @param type
     * @param startCount
     * @param record
     * @param sortBuilderList
     * @return
     */
    public SearchResponse searchRecordListToNativeResult(QueryBuilder queryBuilder, String indexName, String type, int startCount, int record, AggregationBuilder aggregationBuilder, SortBuilder... sortBuilderList);



}
